package com.huawei.dc.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.dc.v3.DcClient;
import com.huaweicloud.sdk.dc.v3.model.CreateHostedDirectConnect;
import com.huaweicloud.sdk.dc.v3.model.CreateHostedDirectConnectRequest;
import com.huaweicloud.sdk.dc.v3.model.CreateHostedDirectConnectRequestBody;
import com.huaweicloud.sdk.dc.v3.model.CreateHostedDirectConnectResponse;
import com.huaweicloud.sdk.dc.v3.model.DeleteHostedDirectConnectRequest;
import com.huaweicloud.sdk.dc.v3.model.DeleteHostedDirectConnectResponse;
import com.huaweicloud.sdk.dc.v3.model.ListHostedDirectConnectsRequest;
import com.huaweicloud.sdk.dc.v3.model.ListHostedDirectConnectsResponse;
import com.huaweicloud.sdk.dc.v3.model.ShowHostedDirectConnectRequest;
import com.huaweicloud.sdk.dc.v3.model.ShowHostedDirectConnectResponse;
import com.huaweicloud.sdk.dc.v3.model.UpdateHostedDirectConnect;
import com.huaweicloud.sdk.dc.v3.model.UpdateHostedDirectConnectRequest;
import com.huaweicloud.sdk.dc.v3.model.UpdateHostedDirectConnectRequestBody;
import com.huaweicloud.sdk.dc.v3.model.UpdateHostedDirectConnectResponse;
import com.huaweicloud.sdk.dc.v3.region.DcRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class HostedDirectConnectionDemo {

    private static final Logger logger = LoggerFactory.getLogger(HostedDirectConnectionDemo.class);
    private static final String AK = "<your AK>";
    private static final String SK = "<your SK>";
    private static final String PROJECT_ID = "<your project id>";

    private static DcClient client = getDcClient();

    /* 获取DC请求客户端 */
    public static DcClient getDcClient() {
        return DcClient.newBuilder()
                .withCredential(new BasicCredentials()
                        .withAk(AK)
                        .withSk(SK)
                        .withProjectId(PROJECT_ID)
                ).withRegion(DcRegion.CN_NORTH_4)
                .build();
    }

    public static void main(String[] args) {

        // 1,创建托管专线
        CreateHostedDirectConnectResponse response = createHostedDirectConnect();

        // 2,查询托管专线详情
        showHostedDirectConnect(response.getHostedConnect().getId());

        // 3,修改托管专线
        updateHostedDirectConnection(response.getHostedConnect().getId());

        // 4,查询托管专线列表
        listHostedDirectConnections(response.getHostedConnect().getId());

        // 5,删除托管专线
        deleteHostedDirectConnection(response.getHostedConnect().getId());
    }

    private static CreateHostedDirectConnectResponse createHostedDirectConnect() {
        CreateHostedDirectConnect createHostedDirectConnect = new CreateHostedDirectConnect()
                .withName("<your hosted-direct-connection name>")
                .withHostingId("<your hostingId>")
                .withDescription("<your hosted-direct-connection description>")
                .withBandwidth(10)
                .withVlan(10)
                .withResourceTenantId("<your resource project id>")
                .withPeerLocation("<your peer location>");
        CreateHostedDirectConnectRequest request = new CreateHostedDirectConnectRequest()
                .withBody(new CreateHostedDirectConnectRequestBody().withHostedConnect(createHostedDirectConnect));
        CreateHostedDirectConnectResponse response = null;
        try {
            response = client.createHostedDirectConnect(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
        return response;
    }

    private static void showHostedDirectConnect(String hostedDirectConnectId) {
        ShowHostedDirectConnectRequest request = new ShowHostedDirectConnectRequest().withHostedConnectId(hostedDirectConnectId);
        try {
            ShowHostedDirectConnectResponse response = client.showHostedDirectConnect(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void updateHostedDirectConnection(String hostedDirectConnectId) {
        UpdateHostedDirectConnectRequest request = new UpdateHostedDirectConnectRequest()
                .withHostedConnectId(hostedDirectConnectId)
                .withBody(new UpdateHostedDirectConnectRequestBody().withHostedConnect(new UpdateHostedDirectConnect()
                        .withName("<your new hosted-direct-connection name>")
                        .withDescription("<your new hosted-direct-connection description>")));
        try {
            UpdateHostedDirectConnectResponse response = client.updateHostedDirectConnect(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void listHostedDirectConnections(String hostedDirectConnectId) {
        ListHostedDirectConnectsRequest request = new ListHostedDirectConnectsRequest().withHostingId(Arrays.asList(hostedDirectConnectId));
        try {
            ListHostedDirectConnectsResponse response = client.listHostedDirectConnects(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void deleteHostedDirectConnection(String hostedDirectConnectId) {
        DeleteHostedDirectConnectRequest request = new DeleteHostedDirectConnectRequest().withHostedConnectId(hostedDirectConnectId);
        try {
            DeleteHostedDirectConnectResponse response = client.deleteHostedDirectConnect(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }
}